function fn(n){
    return n + 1
}

function fn1(n){
    return n + 2
}

function fn2(n){
    return n + 3
}

const a = compose(fn,fn1,fn2)
// console.log(a(1))

function compose(...fn){
    if(!fn.length) return v => v
    if(fn.length === 1) return fn[0]
    return fn.reduce((pre, cur) => {
        (...val) => pre(cur(...val))
    })
}

function mySetInterval(fn,await){
    let timer;
    const interval = () => {
        timer = setTimeout(()=> {
            fn()
            interval()
        }, await)
    }

    interval()

    return {
        cancle:  () => { clearInterval(timer) }
    }
}

function flat(arr){
    if(!arr.length) return
    while(arr.some(item=> Array.isArray(item))){
        arr = [].concat(...arr)
    }
    return arr
}

function flatter(arr){
   return arr.reduce((pre,cur) =>
        Array.isArray(cur) ? [...pre, ...flatter(cur)] : [...pre,cur]
    ,[])
}

function newFn(fn,...args){
    let obj = Object.create(fn.prototype)
    let res = fn.apply(obj,...args)
    if(res && (typeof res === 'object' || typeof res === 'function')){
        return res
    }
    return obj
}



function debounce(fn,immedia,wait){
    let timer
    return function () {
        if(timer){
            clearInterval(timer)
        }

        if(immedia){
            if(!timer){
                fn.apply()
            }
            timer = setTimeout(() => {
                timer = null
            }, wait)
        } else {
            timer = setTimeout(()=>{
                fn.apply()
                timer = null
            }, wait)
        }
    }
}

function throttle(fn,immedia,wait){
    let timer, pre = 0
    return function (){
        if(immedia){
            let now = Date.now()
            if(now - pre > wait){
                fn.apply()
                pre = now
            }
        } else {
            if(!timer){
                timer = setTimeout(() => {
                    fn.apply()
                    timer = null
                }, wait)
            }
        }
    }
}

// 这个冒泡排序法
function sortArray(arr){
    for(let i = 0; i < arr.length - 1; i ++){
        for(let j = i + 1; j < arr.length; j++){
            if(arr[i] > arr[j]){
                let temp = arr[i]
                arr[i] = arr[j]
                arr[j]= temp
            }
        }
    }
    return arr
}
const arr = [2,1,8,31,55,3,5]

console.log(sortArray(arr))

